/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.service;

import java.util.List;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Speciality;
import net.gis.eemc.dao.SpecialityDAO;
import net.gis.eemc.dao.SpecialityDAOImpl;

/**
 *
 * @author Sergey
 */
public class SpecialityServiceImpl {

    private SpecialityDAO specDAO = null;

    public SpecialityServiceImpl() {
        specDAO = new SpecialityDAOImpl();
    }

    public Boolean isSpecialityWithReferenceNumberExists(String inputRefNumber) {
        List<Speciality> spec;
        try {
            spec = specDAO.getSpecialityWithRefNumber(inputRefNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return !spec.isEmpty();
    }

    public Boolean isSpecialityWithTitleExists(String Title) {
        List<Speciality> spec = null;
        try {
            spec = specDAO.getSpecialityWithTitle(Title);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return !spec.isEmpty();
    }

    public Boolean isSpecialityWithShortNameExists(String shortName) {
        List<Speciality> spec;
        try {
            spec = specDAO.getSpecialityWithShortName(shortName);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return !spec.isEmpty();
    }

    public void SaveSpeciality(Department department, String title, String shortName, String refNum) {
        try {
            if (department == null || title == null || shortName == null || refNum == null) {
                throw new NullPointerException("SpecialityServiceImpl::SaveSpeciality one of parameter is null");
            }
            specDAO.addSpeciality(new Speciality(department, title, shortName, refNum));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void UpdateSpeciality(Speciality speciality) {
        try {
            if (speciality == null) {
                throw new NullPointerException("SpecialityServiceImpl::UpdateSpeciality one of parameter is null");
            }
            specDAO.updateSpeciality(speciality);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Speciality> getSpecialities() {
        return specDAO.listSpeciality();
    }

    public List<Speciality> getSpecialities(Faculty fac) {
        if (fac == null) {
            return getSpecialities();
        }

        return specDAO.listSpeciality(fac);
    }

    public List<Speciality> getSpecialities(Department dep) {
        if (dep == null) {
            return getSpecialities();
        }

        return specDAO.listSpeciality(dep);
    }

    public Speciality getSpeciality(Integer id) throws NullPointerException {
        if (id == null)
            throw new NullPointerException("id is null");

        return specDAO.getSpecialityWithID(id);
    }
}
